package com.zzl.rabbitmq.exchange.direct;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.DeliverCallback;
import com.zzl.rabbitmq.utils.RabbitMqUtils;

import java.nio.charset.StandardCharsets;

/**
 * Title: ReceiveLogsDirect01.class<br>
 * Description: <br>
 * Copyright (c)  2021    <br>
 * Create DateTime: 2021/7/9 14:23 <br>
 *
 * @author zhuangzhuangliang
 */
public class ReceiveLogsDirect01 {
    public static final String EXCHANGE_NAME = "direct_logs";

    public static void main(String[] args) throws Exception {

        Channel channel = RabbitMqUtils.getChannel();
        channel.exchangeDeclare(EXCHANGE_NAME, "direct");
        String queueName = "disk";
        channel.queueDeclare(queueName,false,false,false,null);
        channel.queueBind(queueName, EXCHANGE_NAME, "error");

        System.out.println("等待接收消息...");
        DeliverCallback deliverCallback = (consumerTag, message) -> {
            String msg = new String(message.getBody(), StandardCharsets.UTF_8);
            System.out.println("接收到的消息 "+msg + " 绑定的键为 "+message.getEnvelope().getRoutingKey());
            System.out.println("错误日志已接收");
        };
        channel.basicConsume(queueName,true, deliverCallback, consumerTag -> {
        });
    }
}
